package com.ftx.controller.test.annotation;

import com.ftx.controller.test.annotation.condition.LinuxCondition;
import com.ftx.controller.test.annotation.condition.WindowsCondition;
import com.ftx.controller.test.annotation.importTest.*;
import com.ftx.domain.vo.PersonVo;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * Created by fengtingxin on 2020/4/3.
 */
//配置的注解  相当于配置文件
// 在类上配置condition 则这个类的所有的类都是走的这个条件
@Conditional(WindowsCondition.class)
@Configuration
//@ComponentScan(value = "com.ftx", includeFilters = {
//        @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class})
//})
// 指定排除规则  只扫描 controller的包

@Import({Color.class, Red.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
public class MainConfig {

    /**
     * Conditional 按照一定的条件给容器中加载bean
     */
    @Bean(name = "kaifei")
    public PersonVo getPerson04(){
        System.out.println("初始化ben4 ");
        return new PersonVo("kaifei",12);
    }

//    @Conditional(LinuxCondition.class)
    @Scope
    @Bean(name = "zhangsan")
    public PersonVo getPerson01(){
        System.out.println("初始化ben1 ");
        return new PersonVo("zhangsan",12);
    }

//    @Conditional(WindowsCondition.class)
    @Bean(name = "lisi")
    public PersonVo getPerson02(){
        System.out.println("初始化ben2 ");
        return new PersonVo("lisi",12);
    }

    @Bean(name = "wangwu")
    public PersonVo getPerson03(){
        System.out.println("初始化ben3 ");
        return new PersonVo("wangwu",12);
    }



    /**
     * Conditional 按照一定的条件给容器中加载bean
     */
    @Bean(name = "suanmeitang")
    public PersonVo getPerson05(){
        System.out.println("初始化ben4 ");
        return new PersonVo("suanmeitang",12);
    }

    /**
     * 给容器中注册组件的方式
     * 1.包扫描 + 组件标注注解@Controller \ @Service \ @Repository @Component
     * 2. @Bean [导入的第三方包里的组件]
     * 3. @Import[]
     *     1. @Import （要导入到容器的组件） 默认的id是组件的全类名
     *     2. ImportSelector 返回我们需要
     *     3. ImportBeanDefinitionRegistrar :手动注册bean 到容器中
     *  4. FactoryBean  com.ftx.controller.test.annotation.AnnotationTest#testImport()
     */

    @Bean
    public ColorFactory getColorFactory(){
        return new ColorFactory();
    }
}
